package com.example.repair.vm

import com.example.common.view.vm.BaseViewModel
import com.example.repair.api.RepairApiService
import com.example.repair.entity.RepairEntityItem
import com.example.repair.entity.RepairParam
import com.example.repair.model.RepairRepository
import com.example.repair.model.RepairResultData
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.launch
import javax.inject.Inject

@HiltViewModel
class RepairViewModel  @Inject constructor(private val apiService: RepairApiService): BaseViewModel<RepairRepository, RepairResultData<List<RepairEntityItem>>>(){
    fun postRepair(repairParam: RepairParam){
        mViewModelScope.launch (mDispatcher){
            try {
                val result = apiService.postRepair(repairParam)
                val res = RepairResultData(
                    data = listOf(result.data) ,
                    state = result.state,
                    msg = result.msg
                )
                mSuccessLiveData.postValue(res)
            }catch (e:Exception){
                mErrorLiveData.postValue("请求失败:${e.message}")
            }
        }
    }
    fun getRepair(){
        mViewModelScope.launch (mDispatcher){
            try {
                val result = apiService.getRepair()
                val res = RepairResultData(
                    data = result.data ,
                    state = result.state,
                    msg = result.msg
                )
                mSuccessLiveData.postValue(res)
            }catch (e:Exception){
                mErrorLiveData.postValue("请求失败:${e.message}")
            }
        }
    }
}